package cn.ecut.util;

import org.w3c.dom.ls.LSOutput;

import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.Arrays;
import java.util.Objects;
import java.util.Properties;

public class JDBCUtil {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    private static boolean autocommit;
    private static int isolution;

    private static Connection c;

    static {
        config();
    }

    private static void config() {
        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream("src\\main\\resources\\jdbc_config.properties");
            properties.load(inputStream);
            String connect = properties.getProperty("connect");
            autocommit = Boolean.parseBoolean(properties.getProperty("jdbc.connection.transaction.autocommit"));
            isolution = Integer.parseInt(properties.getProperty("jdbc.connection.transaction.isolation"));
            driver = properties.getProperty("jdbc.connection.mysql.driver");
            url = properties.getProperty("jdbc.connection.mysql.url");
            username = properties.getProperty("jdbc.connection.mysql.username");
            password = properties.getProperty("jdbc.connection.mysql.password");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean load() {
        try {
            Class.forName(driver);
            return true;
        } catch (Exception e) {
            System.out.println("加载失败：" + e.getMessage());
        }
        return false;
    }

    public static Connection connect() {
        if (load()) {
            try {
                c = DriverManager.getConnection(url, username, password);
            } catch (SQLException e) {
                System.out.println("连接失败：" + e.getMessage());
            }
        }
        return c;
    }

    public static void autoCommit() {
        if (Objects.isNull(c)) {
            c = connect();
            try {
                c.setAutoCommit(autocommit);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                c.setTransactionIsolation(isolution);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //创建Statement
    public static Statement statement() {
        Statement statement = null;
        if (Objects.isNull(c)) {
            c = connect();
        }
        try {
            statement = c.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return statement;
    }

    //创建PreparedStatement
    public static PreparedStatement preparedStatement(String sql) {
        PreparedStatement preparedStatement = null;
        if (Objects.isNull(c)) {
            c = connect();
        }
        try {
            preparedStatement = c.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return preparedStatement;
    }

    //执行查询语句
    public static ResultSet selectSQL(String SQL, Object... value) {
        ResultSet resultSet = null;
        if (SQL == null || SQL.trim() == null || !SQL.trim().toLowerCase().contains("select")) {
            System.out.println("SQL语句为空或者不是select语句");
        }else{
            if (value.length > 0) {
                PreparedStatement ps = preparedStatement(SQL);
                Connection c = null;
                try {
                    c = ps.getConnection();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {

                    for (int i = 0; i < value.length; i++) {
                        ps.setObject(i + 1, value[i]);
                        System.out.println(value[i]);
                    }
                    resultSet = ps.executeQuery();
                    commit(c);
                } catch (SQLException e) {
                    e.printStackTrace();
                    rollback(c);
                }
            } else {
                Statement ps = statement();
                Connection c = null;
                try {
                    c = ps.getConnection();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    resultSet = ps.executeQuery(SQL);
                    commit(c);
                } catch (SQLException e) {
                    e.printStackTrace();
                    rollback(c);
                }
            }
        }
        return resultSet;
    }

    //执行insert、delete、update等语句
    public static boolean executeSQL(String SQL, Object... value) {
        boolean flag = false;

        if (SQL == null || SQL.trim() == null || SQL.trim().toLowerCase().contains("select")) {
            System.out.println("SQL语句为空或SQL语句为查询语句");
        }else {
            if (value.length > 0) {
                PreparedStatement ps = preparedStatement(SQL);
                try {
                    for (int i = 0; i < value.length; i++) {
                        ps.setObject(i + 1, value[i]);
                    }
                    ps.executeUpdate();
                    commit(c);
                    flag = true;
                } catch (SQLException e) {
                    e.printStackTrace();
                    rollback(c);
                }
            } else {
                throw new RuntimeException("所提供参数不存在，无法执行");
            }
        }
        return flag;
    }

    public static void commit(Connection c) {
        if (c != null && autocommit) {
            try {
                c.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void rollback(Connection c) {
        if (c != null) {
            try {
                c.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
